#include "System.h"
#include "FreeTypeFont.h"
System::System(void)
:pSpriteWithMouse(NULL)
{
	//mEffect.onInit("default.vert","default.frag");
	mRootNode.setName("root node");
}
System::~System(void)
{
}
bool System::injectMouseMove(float delta_x, float delta_y)
{

	return false;
}
bool System::injectMouseLeaves(void)
{
	return false;

}
bool System::injectMouseButtonDown(MouseButton button)
{
	MouseEventArgs args;
	Point position = MouseCursorSingleton::getSingleton()->getPosition();
	args.position = position;
	args.button = button;
	args.wheelChange = 0;
	Node2D* node = mRootNode.getTargetChildAtPosition(position);
	if (node)
	{
		node->onMouseDown(args);
	}else
	{
		mRootNode.onMouseDown(args);
	}
	return false;

}
bool System::injectMouseButtonUp(MouseButton button)
{
	MouseEventArgs args;
	Point position = MouseCursorSingleton::getSingleton()->getPosition();
	args.position = position;
	args.button = button;
	args.wheelChange = 0;
	Node2D* node = mRootNode.getTargetChildAtPosition(position);
	if (node)
	{
		node->onMouseUp(args);
	}else
	{
		mRootNode.onMouseUp(args);
	}
	return false;
}
bool System::injectMouseWheelChange(float delta)
{
	MouseEventArgs args;
	Point position = MouseCursorSingleton::getSingleton()->getPosition();
	args.position = position;
	args.button = MouseButton::NoButton;
	args.wheelChange = delta;
	Node2D* node = mRootNode.getTargetChildAtPosition(position);
	if (node)
	{
		node->onMouseWheel(args);
	}else
	{
		mRootNode.onMouseWheel(args);
	}
	return false;
}
bool System::injectMousePosition(float x_pos, float y_pos)
{
	Point new_position(x_pos,y_pos);
	MouseEventArgs ma;
	ma.moveDelta = new_position - MouseCursorSingleton::getSingleton()->getPosition();

	// no movement means no event
	if ((ma.moveDelta.x == 0) && (ma.moveDelta.y == 0))
		return false;

	//ma.sysKeys = d_sysKeys;
	ma.wheelChange = 0;
	ma.clickCount = 0;
	ma.button = NoButton;

	// move mouse cursor to new position
	MouseCursorSingleton::getSingleton()->setPosition(new_position);
	// update position in args (since actual position may be constrained)
	ma.position = MouseCursorSingleton::getSingleton()->getPosition();
	Node2D* old_node = pSpriteWithMouse;
	Node2D* new_node = mRootNode.getTargetChildAtPosition(new_position);

	if (old_node == new_node)
	{
		return false;
	}
	pSpriteWithMouse = (Sprite*)new_node;
	if (old_node)
	{
		old_node->onMouseOut(ma);
	}
	if (new_node)
	{
		new_node->onMouseOver(ma);
	}
	return false;
}
bool System::injectKeyDown(unsigned int key_code)
{
	printf("==============injectKeyDown:%d\n",key_code);
	return false;

}
bool System::injectKeyUp(unsigned int key_code)
{
	printf("==============injectKeyUp:%d\n",key_code);
	return false;

}
bool System::injectChar(unsigned int code_point)
{
	printf("==============inject char:%d\n",code_point);
	wchar_t buff = (wchar_t)code_point;
	KeyEventArgs args;
	args.codepoint = code_point;
	/*if (pSpriteWithMouse != NULL)
	{
		
		pSpriteWithMouse->onCharacter(args);
	}else
	{
		mRootNode.onCharacter(args);
	}*/
	mRootNode.onCharacter(args);
	return false;

}
Node2D* System::getRootNode()
{
	return &mRootNode;
}

void System::draw(SpriteBatch* spriteBatch)
{
	mRootNode.draw(spriteBatch);
}
